package com.rui.study.algorithm.P_等差数列划分Ⅰ;

import java.util.LinkedList;

/**
 * @program: study
 * @description:
 * @author: Yaowr
 * @create: 2019-01-29 08:04
 **/
public class Solution1_有结果集 {
    public int numberOfArithmeticSlices(int[] A) {
        int n = A.length;
        LinkedList<int[]> state = new LinkedList<>();

        for (int i = 0; i < n - 2; i++) {
            for (int j = i+2; j < n; j++) {
                int p = i;
                int[] seria = new int[j-i+1];
                int diff = A[p+1] - A[p];
                seria[0] = A[p];
                while (p < j) {
                    int curDiff = A[p+1] - A[p];
                    if (curDiff == diff) {
                        seria[p-i+1] = A[p+1];
                        if (p == j-1) {
                            state.add(seria);
                        }
                    } else {
                        break;
                    }
                    p++;
                    diff = curDiff;
                }
            }
        }
        return state.size();
    }



    public static void main(String[] args) {
        int[] a = {1,3,5,7,9};

        long start = System.currentTimeMillis();
        Solution1_有结果集 solution = new Solution1_有结果集();
        int result = solution.numberOfArithmeticSlices(a);
        long end = System.currentTimeMillis();
        System.out.println("等差数列划分：" + result + ", 耗时[" + (end - start) + "ms]");
    }
}
